package com.arch.bigdata.hbase1x.nx;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;

import java.io.IOException;

public class FansGaunzhuCoprocessor extends BaseRegionObserver {

    static Configuration config = HBaseConfiguration.create();
    static HTable table = null;

    static {
        config.set("hbase.zookeeper.quorum", "bigdata02:2181,bigdata03:2181,bigdata04:2181");
        try {
            table = new HTable(config, "guanzhu");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit,
					   Durability durability) throws IOException {
        //		super.prePut(e, put, edit, durability);

        byte[] row = put.getRow();
        Cell cell = put.get("f1".getBytes(), "from".getBytes()).get(0);

        Put putIndex = new Put(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());

        putIndex.addColumn("f1".getBytes(), "from".getBytes(), row);

        table.put(putIndex);
        table.close();
    }
}